import Vue from 'vue'
import VueRouter from 'vue-router'
import { req } from '@/lib/axios.js'
// 导入组件
import Container from '@/router/Container.vue'
import Home from '@/router/Home.vue'
import Category from '@/router/category/Category.vue'
import CategoryGoods from '@/router/category/CategoryGoods.vue'
import Cart from '@/router/Cart.vue'
import User from '@/router/user/User.vue'
import Order from '@/router/order/Order.vue'
import AllOrder from '@/router/order/AllOrder.vue'
import WaitPayOrder from '@/router/order/WaitPayOrder.vue'
import WaitReceiveOrder from '@/router/order/WaitReceiveOrder.vue'
import UserReceiveAddress from '@/router/user/UserReceiveAddress.vue'
import AddUserReceiveAddress from '@/router/user/AddUserReceiveAddress.vue'
import UserDetailInfo from '@/router/user/UserDetailInfo.vue'
import Setting from '@/router/user/Setting.vue'
import ChangePassword from '@/router/user/ChangePassword.vue'
import Search from '@/router/Search.vue'
import GoodsDetail from '@/router/GoodsDetail.vue'
import ConfirmOrder from '@/router/ConfirmOrder.vue'
import Login from '@/router/Login.vue'
import Register from '@/router/Register.vue'
import AdminContainer from '@/router/admin/AdminContainer.vue'
import AddGoods from '@/router/admin/goods/AddGoods.vue'
import SelectGoods from '@/router/admin/goods/SelectGoods.vue'
import { Toast } from 'vant' // 轻提示
// 导入自己的js文件
import { getCookieObj } from '@/utils/utils.js'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/container/home' },
  {
    path: '/container', component: Container, name: 'container',
    children: [
      { path: 'home', component: Home, name: 'home' },
      {
        path: 'category', component: Category, name: 'category',
        children: [
          { path: 'goods', component: CategoryGoods, name: 'categoryGoods' }
        ]
      },
      { path: 'search', component: Search, name: 'search' },
      { path: 'goods/detail', component: GoodsDetail, name: 'goodsDetail' },
      { path: 'cart', component: Cart, name: 'cart' },
      { path: 'user', component: User, name: 'user' },
      { path: 'user/submit/order', component: ConfirmOrder, name: 'submitOrder' },
      {
        path: 'user/order/', component: Order, name: 'order',
        children: [
          { path: 'all', component: AllOrder, name: 'allOrder' },
          { path: 'waitPay', component: WaitPayOrder, name: 'waitPayOrder' },
          { path: 'waitReceive', component: WaitReceiveOrder, name: 'waitReceiveOrder' },
        ]
      }
    ],
  },
  { path: '/user/receive/address', component: UserReceiveAddress, name: 'receiveAddress' },
  { path: '/user/add/receive/address', component: AddUserReceiveAddress, name: 'addReceiveAddress' },
  { path: '/user/userdetailinfo', component: UserDetailInfo, name: 'userdetailinfo' },
  { path: '/user/setting', component: Setting, name: 'setting' },
  { path: '/user/changepassword', component: ChangePassword, name: 'changepassword' },
  { path: '/login', component: Login, name: 'login' },
  { path: '/register', component: Register, name: 'register' },
  {
    path: '/admin', component: AdminContainer, name: 'admin',
    children: [
      { path: 'goods/add', component: AddGoods, name: 'addgoods' },
      { path: 'goods/select', component: SelectGoods, name: 'selectgoods' }
    ],
  },
]

const router = new VueRouter({
  routes
})

// 全局路由守卫
router.beforeEach((to, from, next) => {
  // console.log(to.fullPath.indexOf('/user'));
  if (to.fullPath.indexOf('/user') >= 0 || to.fullPath.indexOf('/cart') >= 0) { // 如果要前往的路由地址和用户相关需要进行登录
    // cookies = getCookieObj()
    req.get(
      'user/login/',
      {
        params: {
          token: localStorage.getItem('token')
        }
      }
    ).then((res) => {
      // console.log(res.data)
      if (res.data.mark === 1) {
        next()
      } else {
        router.push({ name: 'login' })
      }
    }, err => {
      Toast.fail(err)
    })
  } else {
    next()
  }
})

export default router
